package com.example.administrator.fastdream.date.presenter;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.example.administrator.fastdream.date.bean.BaseBean;
import com.example.administrator.fastdream.date.bean.LookDreamBean;
import com.example.administrator.fastdream.date.bean.UploadPhotoBean;
import com.example.administrator.fastdream.date.http.DialogCallback;
import com.example.administrator.fastdream.date.http.JsonCallback;
import com.example.administrator.fastdream.date.utils.CacheUtils;
import com.example.administrator.fastdream.date.utils.MyUrl;
import com.example.administrator.fastdream.view.activity.LoginActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.util.List;

public class DreamPresenter {
    private Activity mActivity;
    private DreamCallBack callBack;

    public DreamPresenter(Activity activity, DreamCallBack callBack) {
        this.mActivity = activity;
        this.callBack = callBack;
    }

    public void uploadPhotos(List<File> files) {
        HttpParams params = new HttpParams();
        for (int i = 0; i < files.size(); i++) {
            params.put("files[]", files.get(i));
        }
        OkGo.<UploadPhotoBean>post(MyUrl.BASEURL + "Home/upload_img")
                .tag(this)
                .params(params)
                //                .addFileParams("files", files)//这种不好使，看来跟PHP那边的写法有关系
                .execute(new DialogCallback<UploadPhotoBean>(mActivity, UploadPhotoBean.class) {
                    @Override
                    public void onSuccess(Response<UploadPhotoBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            callBack.uploadPhotos(response.body());
                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public void saveDream(String dream_name, String dream_money, String dream_declaration, String images) {
        HttpParams params = new HttpParams();
        params.put("user_token", (String) CacheUtils.get("token"));
        params.put("dream_name", dream_name);
        params.put("dream_money", dream_money);
        params.put("dream_declaration", dream_declaration);
        params.put("images", images);
        OkGo.<BaseBean>post(MyUrl.BASEURL + "Home/consummate_dream")
                .tag(this)
                .params(params)
                .execute(new DialogCallback<BaseBean>(mActivity, BaseBean.class) {
                    @Override
                    public void onSuccess(Response<BaseBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            callBack.saveDream(response.body());
                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public void editDream(String dream_name, String dream_money, String dream_declaration, String images) {
        HttpParams params = new HttpParams();
        params.put("user_token", (String) CacheUtils.get("token"));
        params.put("dream_name", dream_name);
        params.put("dream_money", dream_money);
        params.put("dream_declaration", dream_declaration);
        params.put("images", images);
        OkGo.<BaseBean>post(MyUrl.BASEURL + "Home/edit_dream")
                .tag(this)
                .params(params)
                .execute(new DialogCallback<BaseBean>(mActivity, BaseBean.class) {
                    @Override
                    public void onSuccess(Response<BaseBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            callBack.saveDream(response.body());
                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public void lookDream() {
        HttpParams params = new HttpParams();
        params.put("user_token", (String) CacheUtils.get("token"));
        OkGo.<LookDreamBean>post(MyUrl.BASEURL + "Home/show_dream")
                .tag(this)
                .params(params)
                .execute(new JsonCallback<LookDreamBean>(LookDreamBean.class) {
                    @Override
                    public void onSuccess(Response<LookDreamBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            callBack.lookDream(response.body());
                        } else if (status.equals("2")) {
                            if (response.body().getMessage().equals("暂无数据")) {
                                callBack.lookDream(response.body());
                            } else {
                                Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public interface DreamCallBack {
        void saveDream(BaseBean baseBean);

        void uploadPhotos(UploadPhotoBean uploadPhotoBean);

        void lookDream(LookDreamBean lookDreamBean);
    }
}
